<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="维护类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择维护类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="计划类型" prop="scheduleType">
        <el-select v-model="queryParams.scheduleType" placeholder="请选择计划类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="计划执行时间" prop="scheduleTime">-->
        <!--<el-date-picker clearable size="small"-->
          <!--v-model="queryParams.scheduleTime"-->
          <!--type="date"-->
          <!--value-format="yyyy-MM-dd"-->
          <!--placeholder="选择计划执行时间">-->
        <!--</el-date-picker>-->
      <!--</el-form-item>-->
      <el-form-item label="责任人" prop="responsible">
        <el-input
          v-model="queryParams.responsible"
          placeholder="请输入责任人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="状态" prop="state">-->
        <!--<el-input-->
          <!--v-model="queryParams.state"-->
          <!--placeholder="请输入状态"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="字段1" prop="field1">-->
        <!--<el-input-->
          <!--v-model="queryParams.field1"-->
          <!--placeholder="请输入字段1"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="字段2" prop="field2">-->
        <!--<el-input-->
          <!--v-model="queryParams.field2"-->
          <!--placeholder="请输入字段2"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="字段3" prop="field3">-->
        <!--<el-input-->
          <!--v-model="queryParams.field3"-->
          <!--placeholder="请输入字段3"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="模块类型" prop="classifyType">-->
        <!--<el-select v-model="queryParams.classifyType" placeholder="请选择模块类型" clearable size="small">-->
          <!--<el-option label="请选择字典生成" value="" />-->
        <!--</el-select>-->
      <!--</el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['security:maintainOrder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['security:maintainOrder:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['security:maintainOrder:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['security:maintainOrder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="maintainOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="维护类型" align="center" prop="type" />
      <el-table-column label="计划类型" align="center" prop="scheduleType" />
      <el-table-column label="计划执行时间" align="center" prop="scheduleTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.scheduleTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="责任人" align="center" prop="responsible" />
      <el-table-column label="状态" align="center" prop="state" />
      <!--<el-table-column label="字段1" align="center" prop="field1" />-->
      <!--<el-table-column label="字段2" align="center" prop="field2" />-->
      <!--<el-table-column label="字段3" align="center" prop="field3" />-->
      <!--<el-table-column label="模块类型" align="center" prop="classifyType" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['security:maintainOrder:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['security:maintainOrder:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改维护计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--<el-form-item label="序号" prop="id">-->
          <!--<el-input v-model="form.id" placeholder="请输入序号" />-->
        <!--</el-form-item>-->
        <el-form-item label="维护类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择维护类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="计划类型" prop="scheduleType">
          <el-select v-model="form.scheduleType" placeholder="请选择计划类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="计划执行时间" prop="scheduleTime">
          <el-date-picker clearable size="small"
            v-model="form.scheduleTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择计划执行时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="责任人" prop="responsible">
          <el-input v-model="form.responsible" placeholder="请输入责任人" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态" />
        </el-form-item>
        <!--<el-form-item label="字段1" prop="field1">-->
          <!--<el-input v-model="form.field1" placeholder="请输入字段1" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="字段2" prop="field2">-->
          <!--<el-input v-model="form.field2" placeholder="请输入字段2" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="字段3" prop="field3">-->
          <!--<el-input v-model="form.field3" placeholder="请输入字段3" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="模块类型" prop="classifyType">-->
          <!--<el-select v-model="form.classifyType" placeholder="请选择模块类型">-->
            <!--<el-option label="请选择字典生成" value="" />-->
          <!--</el-select>-->
        <!--</el-form-item>-->
        <el-divider content-position="center">维护单项信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddMaintainItem">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteMaintainItem">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="maintainItemList" :row-class-name="rowMaintainItemIndex" height="120px" @selection-change="handleMaintainItemSelectionChange" ref="maintainItem">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="标题" prop="label">
            <template slot-scope="scope">
              <el-input v-model="scope.row.label" placeholder="请输入标题" />
            </template>
          </el-table-column>
          <el-table-column label="标签" prop="tag">
            <template slot-scope="scope">
              <el-input v-model="scope.row.tag" placeholder="请输入标签" />
            </template>
          </el-table-column>
          <el-table-column label="标签图标" prop="tagIcon">
            <template slot-scope="scope">
              <el-input v-model="scope.row.tagIcon" placeholder="请输入标签图标" />
            </template>
          </el-table-column>
          <el-table-column label="默认值" prop="defaultvalue">
            <template slot-scope="scope">
              <el-input v-model="scope.row.defaultvalue" placeholder="请输入默认值" />
            </template>
          </el-table-column>
          <!--<el-table-column label="字段1" prop="field1">-->
            <!--<template slot-scope="scope">-->
              <!--<el-input v-model="scope.row.field1" placeholder="请输入字段1" />-->
            <!--</template>-->
          <!--</el-table-column>-->
          <!--<el-table-column label="字段2" prop="field2">-->
            <!--<template slot-scope="scope">-->
              <!--<el-input v-model="scope.row.field2" placeholder="请输入字段2" />-->
            <!--</template>-->
          <!--</el-table-column>-->
          <!--<el-table-column label="字段3" prop="field3">-->
            <!--<template slot-scope="scope">-->
              <!--<el-input v-model="scope.row.field3" placeholder="请输入字段3" />-->
            <!--</template>-->
          <!--</el-table-column>-->
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMaintainOrder, getMaintainOrder, delMaintainOrder, addMaintainOrder, updateMaintainOrder } from "@/api/smartParks/security/maintainOrder";

export default {
  name: "MaintainOrder",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedMaintainItem: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维护计划表格数据
      maintainOrderList: [],
      // 维护单项表格数据
      maintainItemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        scheduleType: null,
        scheduleTime: null,
        responsible: null,
        state: null,
        field1: null,
        field2: null,
        field3: null,
        classifyType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询维护计划列表 */
    getList() {
      this.loading = true;
      listMaintainOrder(this.queryParams).then(response => {
        this.maintainOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        scheduleType: null,
        scheduleTime: null,
        responsible: null,
        state: null,
        field1: null,
        field2: null,
        field3: null,
        classifyType: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.maintainItemList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加维护计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMaintainOrder(id).then(response => {
        this.form = response.data;
        this.maintainItemList = response.data.maintainItemList;
        this.open = true;
        this.title = "修改维护计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.maintainItemList = this.maintainItemList;
          if (this.form.id != null) {
            updateMaintainOrder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMaintainOrder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除维护计划编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMaintainOrder(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
	/** 维护单项序号 */
    rowMaintainItemIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 维护单项添加按钮操作 */
    handleAddMaintainItem() {
      let obj = {};
      obj.label = "";
      obj.tag = "";
      obj.tagIcon = "";
      obj.defaultvalue = "";
      obj.field1 = "";
      obj.field2 = "";
      obj.field3 = "";
      this.maintainItemList.push(obj);
    },
    /** 维护单项删除按钮操作 */
    handleDeleteMaintainItem() {
      if (this.checkedMaintainItem.length == 0) {
        this.$alert("请先选择要删除的维护单项数据", "提示", { confirmButtonText: "确定", });
      } else {
        this.maintainItemList.splice(this.checkedMaintainItem[0].index - 1, 1);
      }
    },
    /** 单选框选中数据 */
    handleMaintainItemSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.maintainItem.clearSelection();
        this.$refs.maintainItem.toggleRowSelection(selection.pop());
      } else {
        this.checkedMaintainItem = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('security/maintainOrder/export', {
        ...this.queryParams
      }, `security_maintainOrder.xlsx`)
    }
  }
};
</script>
